<?php

// +----------------------------------------------------------------------
// | Admin Plugin for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2024 ThinkAdmin [ thinkadmin.top ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/think-plugs-admin
// | github 代码仓库：https://github.com/zoujingli/think-plugs-admin
// +----------------------------------------------------------------------

declare(strict_types=1);

namespace app\paths\controller;

use app\common\model\paths\ClassTaskModel;
use app\common\model\paths\EmployInfoModel;
use app\common\model\paths\EmployLogModel;
use app\common\model\paths\EmployModel;
use app\common\model\paths\TaskChapterModel;
use app\common\model\paths\TaskModel;
use app\common\model\paths\UserClassModel;
use app\common\model\paths\UserStudyGoldModel;
use app\common\model\index\UserModel;
use think\admin\Controller;
use think\admin\extend\DataExtend;
use think\admin\helper\QueryHelper;
use think\facade\Validate;
use think\file\UploadedFile;

/**
 * 学生成绩查询
 * @class Base
 * @package app\admin\controller
 */
class UserGold extends Controller
{
    /**
     * 学生成绩查询
     *
     */
    public function index ()
    {

        $this->class_id = $this->request->get('class_id');

        UserStudyGoldModel::mQuery()->layTable(function () {

            $task_ids       = ClassTaskModel::mk()->where('class_id', $this->class_id)->column('task_id');

            $this->task_arr =  TaskModel::mk()->whereIn('id', $task_ids)->column('title', 'id');
            $this->task_id  = $this->request->get('task_id', $task_ids[0] ?? 0);

        }, function (QueryHelper $query) {

            $this->task_id  = $this->request->get('task_id');
            $query->where('class_id', $this->class_id);
            $query->where('task_id', $this->task_id);
            $query->group('index_user_id');
            $query->field('index_user_id, sum(total_gold) as total_gold');
        });

    }

    protected function _index_page_filter(&$data)
    {

        array_map(function ($item) use (&$index_user_gold){
            $index_user_gold[$item['index_user_id']] = $item['total_gold'];
        }, $data);

        // 查询申请班级的人
        $index_user_arr = UserClassModel::where('class_id',$this->class_id)->where('status', UserClassModel::STATUS_AGREE)->column('index_user_id');

        $user_name = UserModel::mk()->whereIn('id', $index_user_arr)->column('true_name', 'id');

        $new_data = [];
        foreach ($user_name as $key => $name) {

            $new_data[] = [
                'user_name'     => $name,
                'total_gold'    => $index_user_gold[$key] ?? 0,
                'index_user_id' => $key,
            ];
        }
        $data = $new_data;
    }



    public function info ()
    {

        $this->index_user_id = $this->request->get('index_user_id', 0);

        $this->task_id       = $this->request->get('task_id', 0);


        EmployLogModel::mQuery()->layTable(function () {


        }, function (QueryHelper $query) {


            $query->where('index_user_id', $this->index_user_id);
            $query->where('task_id', $this->task_id);
            $query->where('gold', '>', 0);
        });
    }

    protected function  _info_page_filter (&$data)
    {

        $employ_ids = $task_ids = [];
        array_map(function ($item) use (&$employ_ids , &$task_ids) {

            if ($item['type'] == 1) {

                $employ_ids[] = $item['levels_id'];
            } else {
                $task_ids[]   = $item['levels_id'];
            }

        }, $data);

        // 找到 实战  和 课程 信息
        $employ_title = EmployInfoModel::mk()->whereIn('id', $employ_ids)->column('title', 'id');
        $task_title   = TaskChapterModel::mk()->whereIn('id', $task_ids)->column('title', 'id');

        $type_name = EmployLogModel::getTypeName();

        foreach ($data as &$item) {

            $item['type_name'] = $type_name[$item['type']];

            $item['title']     = $item['type'] == 1 ? $employ_title[$item['levels_id']] : $task_title[$item['levels_id']];
        }
    }
}